using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Gj
{

    public class ColorAnimationHelper : AnimationHelper
    {
        public enum ColorAnimationType
        {
            None,
            Image,
            Text,
            TextPro,
            Material
        }
        
        public Image image;
        public Text text;
        public TextMeshProUGUI textPro;
        public Renderer renderer;
        public string materialColorText = "_Color";
        public ColorAnimationType type = ColorAnimationType.None;
        public AnimationCurve curve = AnimationCurve.Linear(0, 0, 1, 1);
        public Color start;
        public Color end;

        protected override void Init()
        {
            switch (type)
            {
                case ColorAnimationType.Text:
                    if (text == null) text = GameObject.GetComponent<Text>();
                    break;
                case ColorAnimationType.TextPro:
                    if (textPro == null) textPro = GameObject.GetComponent<TextMeshProUGUI>();
                    break;
                case ColorAnimationType.Image:
                    if (image == null) image = GameObject.GetComponent<Image>();
                    break;
                case ColorAnimationType.Material:
                    if (renderer == null) renderer = GameObject.GetComponent<Renderer>();
                    break;
            }
        }

        Color GetValue(float value)
        {
            return Color.Lerp(start, end,  easeType == iTween.EaseType.linear ? curve.Evaluate(value) : value);
        }

        protected override void OnUpdate(float value)
        {
            Color c = GetValue(value);
            switch (type)
            {
                case ColorAnimationType.Text:
                    text.color = c;
                    break;
                case ColorAnimationType.TextPro:
                    textPro.color = c;
                    break;
                case ColorAnimationType.Image:
                    image.color = c;
                    break;
                case ColorAnimationType.Material:
                    renderer.material.SetColor(materialColorText, c);
                    break;
            }
        }
    }
}